        TITL DAI FIRMWARE C035-C1FA  V1.1
        ORG   :C035
*
*
*
*
********************************
* MATH. PACKAGE INITIALISATION *
********************************
*
* Entry: HL: Address input encoding routine (EFETCH).
*        DE: Base address error routines (MEVEC).
* Exit:  AFDEHL corrupted, BC preserved.
*
MINIT   SHLD  AGETC     Init. (AGETC/AGETC+1)=EFETCH
        XCHG
        SHLD  EVECT     Init. (EVECT/EVECT+1)=MEVEC
        LDA   MCMDST    Get math.chip status
        ORA   A         Check if math.chip present
        MVI   A,:00     Flag = 0 if not
        JM    LC18    
        MVI   A,:7B     Flag = #7B if present
LC18    STA   MVECA     Set math.chip flag
        RET 
*
**************************
* OVERFLOW ERROR ROUTINE *
**************************
*
* (From LC20 common part for various entries).
*
* Jump to (EVECT/EVECT+1) = Address 'overflow error'
* routine (MEVEC).
*
* Entry: If start at LC20: offset in HL.
* Exit:  AFBCDEHL preserved.
*        On stack original return address.
*
FPEOV   PUSH  H
        LXI   H,:0000   Init offset = 0
*
LC20    PUSH  PSW
        PUSH  D
        XCHG            Offset in DE
        LHLD  EVECT     Get addr pointer
        DAD   D         Add offset
        MOV   A,M
        INX   H
        MOV   H,M
        MOV   L,A       Get addr routine in HL
        POP   D
        POP   PSW
        XTHL            New addr on stack
        RET             Continu with new address
*
******************
* ARGUMENT ERROR *
******************
*
* Jump to  
* (EVECT/EVECT+1)+2 = Address 'number out of range'
* routine (MEVEC+2).
*
* Entry/exit: See FPEOV.
*
FPEAE   PUSH  H
        LXI   H,:0002   Init. offset = 2
        JMP   LC20      Calc. new addr, go to it
*
*******************
* UNDERFLOW ERROR *
*******************
*
* Jump to (EVECT/EVECT+1)+4 = Return (MEVEC+4).
* Underflow gives 0 as result of operation.
*
* Entry/exit: See FPEOV.
*
FPEUN   PUSH  H
        LXI   H,FP0     Addr. FPT(0)
        JMP   LD21      Copy '0' into MACC
*
************************
* DIVIDE BY ZERO ERROR *
************************
*
* Jump to 
* (EVECT/EVECT+1)+6 = Address 'divide by zero'
* routine (MEVEC+6).
*
* Entry/exit: See FPEOV.
*
FPED0   PUSH  H
        LXI   H,:0006   Init. offset = 6
        JMP   LC20      Calc. new addr, go to it
*
*
***************************
* GET CHARACTER FROM LINE *
***************************
*
* Entry: None.
* Exit : All registers preserved.
*        Address to continue on stack.
*
LC22    PUSH  H
        LHLD  AGETC     Get addr 'Get char' routine
        XTHL            on stack; restore HL
        RET             Goto (EVECT/EVECT+1)+2
*
**************************
* FLOATING POINT COMPARE *
**************************
*
* Compares normalised FPT numbers in MACC
* and in M.
*
* Exit: ABCDEHL preserved.
*       Flags: CY=1,S=0,Z=1: both nrs. 0
*              CY=0,S=0,Z=1: both nrs. identical
*              CY=0,S=0,Z=0: MACC > M
*              CY=0,S=1,Z=0: MACC < M
*
FCOMP   PUSH  B
        PUSH  PSW
        PUSH  D
        PUSH  H
        RST   4         Copy MACC to reg A,B,C,D
        DATA  :15
        MOV   E,A       Exp.byte in E
        XRA   M         XOR both exp.bytes
        JM    LC241     Jump if different signs
 REM
* If equal signs:
 REM
        JMP   LD17      Goto LD17, return to LC23
*
LC23    RAL 
        JNZ   LC27    
LC24    MOV   A,E
        SUB   M         Comp. exp. bytes
        JNZ   LC26      Jump if not equal
        INX   H
        MOV   A,B
        SUB   M         Comp. 1st bytes mantissa's
        JNZ   LC26      Jump if not equal
        INX   H
        MOV   A,C
        SUB   M         Comp. 2nd bytes mantissa's
        JNZ   LC26      Jump if not equal
        INX   H
        MOV   A,D
        SUB   M         Comp. 3rd bytes mantissa's
LC25    JZ    LC29      Jump if not equal
LC26    RAR             ) Set flags for output
LC27    XRA   E         )
LC28    ORI   :01       Clear CY-flag
LC29    POP   H
        POP   D
        POP   B
        MOV   A,B       Restore A
        POP   B
        RET 
*
*******************
* INTEGER COMPARE *
*******************
*
* Compares INT numbers in MACC and M.
* REMARK: Routine is incorrect when both
*         numbers are negative ! Then result
*         is if MACC > M due to LC26/LC27.
*
* Exit:  ABCDEHL preserved. CY=0
*        Flags: S=0, Z=1: Both numbers equal
*               S=0, Z=0: MACC > M
*               S=1, Z=0: MACC < M
*
ICOMP   PUSH  B
        PUSH  PSW
        PUSH  D
        PUSH  H
        RST   4         Copy MACC to reg. A,B,C,D
        DATA  :15
        MOV   E,A       Sign byte in E
        XRA   M         XOR both sign bytes
*
*
*******************
* New in V1.1     *
*******************
*
* The temporary storage register E (sign byte) is
* cleared to avoid problems when using the combined
* FPT/INT exit LC27. This is done to remove a bug in
* comparing 2 relational numeric operations. The
* unitary operator '-' is now recognised correctly.
*
        JMP   CLRE      Modif V1.1
*
 REM
* If different signs:
 REM
LC241   XRA   M         Find out which one is neg:
 REM S=1: MEM pos; MACC neg
 REM S=0: MEM neg; MACC pos
        JMP   LC28      Abort
*
**************************************
* INCREMENT INTEGER NUMBER IN MEMORY *
**************************************
*
* Entry: HL points to 1st byte of INT number.
* Exit:  All registers preserved.
*
IINM    PUSH  PSW
        PUSH  H
        INX   H
        INX   H
        INX   H         HL pnts to last byte
        MVI   A,:03     Nr of bytes for INT nr
LC30    INR   M         Incr. INT nr.
        JNZ   LC31      Ready if no overflow
        DCX   H         Goto next byte
        DCR   A         1st byte reached?
        JNZ   LC30      Incr. next byte
        INR   M         Incr. 1st byte
        MOV   A,M       Get it
        CPI   :80       msb=1?
        CZ    FPEOV     Then overflow error
LC31    POP   H         Normal return
        POP   PSW
        RET 
*
***************************************************
* DECREMENT INTEGER NUMBER IN MEMORY - (not used) *
***************************************************
*
* Entry: HL points to 1st byte of INT number.
* Exit:  All registers preserved.
*
IDCM    PUSH  PSW
        PUSH  B
        PUSH  H
        INX   H
        INX   H
        INX   H         HL pnts to last byte
        MVI   B,:03     Nr. of bytes of INT nr.
LC32    DCR   M         Decr. INT nr
        MOV   A,M
        INR   A         Check for overflow
        JNZ   LC33      Ready if no overflow
        DCX   H         Goto next byte
        DCR   B         Decr. byte count
        JNZ   LC32      Next byte if not ready
        DCR   M         Decr. hibyte
        MOV   A,M
        CPI   :7F       Check for overflow
        CZ    FPEOV     Then run overflow error
LC33    POP   H         Normal return
        POP   B
        POP   PSW
        RET 
*
*********************************************
* INCREMENT FLOATING POINT NUMBER IN MEMORY *
*********************************************
*
* If number = 0, or exponent < 0, a '1' is added
* to the lsb of the mantissa. Else, the position
* of the least significant '1' is looked up, and
* a '1' is added to this position.
* If the lsb of the mantissa is already a rounded
* value, no increment occurs.
*
* Entry: HL points to 1st byte of FPT number.
* Exit:  All registers preserved.
*
FINM    PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        LXI   D,FP1     Addr FPT(1)
        MOV   A,M       Get exp.byte
        ANI   :7F       Mask sign bit
        JZ    LC45      If nr=0: add 1, abort
        CPI   :40       Is exponent negative?
        JNC   LC45      Then add 1, abort
        CPI   :19       Lsb of mantissa is not lsb
 REM of number ?
        JNC   EXIT      Then Popall, ret
        CMP   M         Check if nr. is negative
        INX   H
        JNZ   LC39      Then jump
 REM
* From LC34 also used by XFDCM.
* Find lsb of mantissa if nr is positive:
 REM
LC34    SUI   :09       In 1st byte ?
        CC    LC50      Then SHL bit into A (A) time
        JC    LC36      and jump
        INX   H
        SUI   :08       In 2nd byte ?
        CC    LC50      Then SHL bit into A (A) time
        JC    LC35      and jump
        INX   H
        SUI   :08       In 3rd byte ?
        CALL  LC50      Then SHL bit into A (A) time
        ADD   M
        MOV   M,A       Add 1 to 3rd byte mantissa
        JNC   EXIT      Ready if no overflow
        DCX   H
        MVI   A,:01     Overflow: add 1 to 2nd byte
LC35    ADD   M
        MOV   M,A       Add 1 to 2nd byte mantissa
        JNC   EXIT      Ready if no overflow
        DCX   H
        MVI   A,:01     Overflow: add 1 to 1st byte
LC36    ADD   M
        MOV   M,A       Add 1 to 1st byte mantissa
        JNC   EXIT      Ready if no overflow
 REM
* If overflow into exponent byte:
 REM
        RAR             )
        MOV   M,A       )
        INX   H         )
        MOV   A,M       ) Shift all bits in
        RAR             ) mantissa right
        MOV   M,A       ) one position
        INX   H         )
        MOV   A,M       )
        RAR             )
        MOV   M,A       )
        DCX   H
        DCX   H
        DCX   H         HL pnts to exp.byte
        MVI   A,:01
LC37    CALL  LC46      Add 1 to exponent
*
EXIT    POP   H
        POP   D
        POP   B
        POP   PSW
        RET 
 REM
* Find lsb of mantissa if nr is negative:
 REM
LC39    SUI   :09       In 1st byte ?
        CC    LC50      Then SHL bit into A (A) time
        JC    LC41      and jump
        INX   H
        SUI   :08       In 2nd byte ?
        CC    LC50      Then SHL bit into A (A) time
        JC    LC40      and jump
        INX   H
        SUI   :08       In 3rd byte ?
        CC    LC50      Then SHL bit into A (A) time
        MOV   B,A
        MOV   A,M
        SUB   B         Subtract 1 from 3rd byte
        MOV   M,A
        JNC   EXIT      Ready if no borrow
        DCX   H
        MVI   A,:01     Subtract 1 from 2nd byte if
 REM borrow
LC40    MOV   B,A
        MOV   A,M
        SUB   B         Subtract 1 from 2nd byte
        MOV   M,A
        JNC   EXIT      Ready if no borrow
        DCX   H
        MVI   A,:01     Subtract 1 from 1st byte if
 REM borrow
LC41    MOV   B,A
        MOV   A,M
        SUB   B         Subtract 1 from 1st byte
        MOV   M,A
        JM    EXIT      Ready if normalised
 REM
* If not normalised:
 REM
        MVI   B,:18     Nr of mantissa bits
LC42    INX   H         )
        INX   H         )
        ORA   A         )
        MOV   A,M       )
        RAL             )
        MOV   M,A       ) Shift all bits
        DCX   H         ) of mantissa
        MOV   A,M       ) left one position
        RAL             )
        MOV   M,A       )
        DCX   H         )
        MOV   A,M       )
        RAL             )
        MOV   M,A       )
        ORA   A
        JM    LC43      If normalized
        DCR   B         Update exp. count
        JZ    LC44      If exp. now zero
        JMP   LC42      Cont. normalisation
 REM
* Normalisation done:
 REM
LC43    DCX   H         Pnts to exp.byte
        MOV   A,B       Get exp. count
        SUI   :19       Minus nr of bytes in
 REM mantissa
        JMP   LC37      Update exponent, quit
 REM
* If exponent is zero:
 REM
LC44    DCX   H
        MVI   M,:00     Exp.byte is 0
        JMP   EXIT      Popall, ret
 REM
* Simply add 1 (FINM) or add -1 (FDCM):
 REM
LC45    RST   4         Copy number into MACC
        DATA  :0C
        XCHG
        RST   4         Add 1 or -1 (FPT)
        DATA  :00
        XCHG
        RST   4         Copy MACC into memory
        DATA  :0F
        JMP   EXIT      Popall, ret
*
*****************
* ADD EXPONENTS *
*****************
*
* LC225 for operand in MACC.
* LC46 for operand in M.
*
* Entry: Byte to be added to exponent in A.
* Exit:  BCDEHL preserved.
*        CY=0: O.K.
*        CY=1: Overflow.
*
LC225   LXI   H,FPACH   Addr. MACC
*
LC46    PUSH  H
        PUSH  D
        PUSH  B
        MOV   C,A       Byte to be added in C
        MOV   A,M       Get exp.byte operand
        ANI   :80       Sign bit mantissa only
        MOV   B,A       in B
        MOV   A,M       Get exp.byte
        CALL  SEXT      Sign extend
        PUSH  PSW       Save sign extended exp.byte
        XRA   C         XOR with byte to be added
        CMA 
        MOV   D,A
        POP   PSW       Get sign extended exp.byte
        ADD   C         Add byte to exponent
        MOV   C,A       Store result
        RAR 
        XRA   C
        ANA   D
        JM    LC48      If overflow into sign bit
        MOV   A,C       Get new exp.byte
        RAL 
        XRA   C
        JM    LC48      If overflow into sign bit
        MOV   A,C       Get new exp.byte
        ANI   :7F       Exponent only
        ORA   B         Add sign bit mantissa
        MOV   M,A       Store it
LC47    POP   B
        POP   D
        POP   H
        RET             CY=0
 REM
* If overflow into sign bit:
 REM
LC48    MOV   A,C       Get new exp.byte
        RAL 
        ORA   A
        STC 
        JMP   LC47      Abort with CY=1
*
***************
* SIGN EXTEND *
***************
*
* Exponent byte is normalized.
*
* Entry: Exp.byte in A.
* Exit:  BCDEHL preserved.
*        Normalized exp.byte in A: Exp.value in
*        bits 7-2, sign mantissa in bit 1, sign
*        exponent in bit 0.
*
SEXT    RLC 
        RLC 
        RRC 
        RAR 
        RET 
*
************************************
* MOVE A BIT INTO A LEFT (A) TIMES *
************************************
*
* Used to place a '1' in the correct position in
* a byte for adding/subtracting '1' to/from the
* least significant '1' of a FPT mantissa.
*
* Entry: A contains a neg. number indicating
*        how often RAL has to be performed.
* Exit:  Result in A,B.
*        FCDEHL preserved.
*
LC50    PUSH  PSW
        MOV   B,A       Save nr of shifts
        XRA   A         Clear A
        STC             Set CY
LC51    RAL             SHL
        INR   B         Update count
        JNZ   LC51      Continu if not ready
        MOV   B,A       Save result
        POP   PSW
        MOV   A,B       Result in A
        RET 
*
*
*
        UNL
AGETC   EQU   :00D2
EVECT   EQU   :00D0
MCMDST  EQU   :FB02
MVECA   EQU   :00D4
FP0     EQU   :C45E
LD21    EQU   :D20C
LD17    EQU   :D1E8
CLRE    EQU   :D1C8
FP1     EQU   :C462
FPACH   EQU   :00D5
        LST
        END
